package com.homelock.common.config;

import com.pi.base.exception.ServiceException;
import org.apache.commons.lang3.StringUtils;
import org.apache.tomcat.util.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Configuration//这是一个组件类，写上这个注解，SPringBoot会在启动时加载它
public class LockInitParam {

	private static final Logger logger = LoggerFactory.getLogger(LockInitParam.class);
	private String server;
	/**
	 * 秘钥
	 */
	@Value("${lock.secretkey}")
	private String secretkey;

	private byte[] secretKeyBytes;

	public String getServer() {
		return server;
	}

	public void setServer(String server) {
		this.server = server;
	}

	public byte[] getSecretkeyByte(){
		if(secretKeyBytes != null){
			return secretKeyBytes;
		}
		logger.debug("当前服务器的key值：{}",secretkey);
			if(StringUtils.isEmpty(secretkey)){
				throw new ServiceException("服务器秘钥获取失败");
			}
		secretKeyBytes = Base64.decodeBase64(secretkey);
			return secretKeyBytes;
	}
}
